package com.hls.mybatis.core.database;

import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

public class DBHelper {


  private static String username;
  private static String password;
  private static String url;
  private static String driver;
  static
  {
      configure("app.properties");
      try
      {
          Class.forName(driver);
          
      } catch (ClassNotFoundException e)
      {
          e.printStackTrace();
      }
  }
  
  public static void configure(String path)
  {
      InputStream in = DBHelper.class.getClassLoader().getResourceAsStream(path);
      Properties p = new Properties();
      try
      {
          p.load(in);
          username = p.getProperty("jdbc.username");
          password = p.getProperty("jdbc.password");
          url = p.getProperty("jdbc.url");
          driver = p.getProperty("jdbc.driver");
          
      } catch (IOException e)
      {
          e.printStackTrace();
      }
  }
  
  
  public static Connection getConnection()
  {
      Connection conn = null;
      try
      {
          conn = DriverManager.getConnection(url,username,password);
      }catch (SQLException e)
      {
          e.printStackTrace();
      }
      return conn;
  }
  public static void closeConnection(Connection connection)
  {
      try
      {
          if(connection != null)
          {
              connection.close();
              connection = null;
          }
      }  catch (SQLException e)
      {
          e.printStackTrace();
      }
  }
  
  public static PreparedStatement prepareStatement(Connection connection,String sql)
  {
      PreparedStatement preparedStatement = null;
      try
      {    
          preparedStatement = connection.prepareStatement(sql);
      } catch (SQLException e)
      {
          e.printStackTrace();
      }
      return preparedStatement;
  }
  public static void closePreparedStatement(PreparedStatement preparedStatement)
  {
      try
      {
          if(preparedStatement != null)
          {
              preparedStatement.close();
              preparedStatement = null;
          }
      }  catch (SQLException e)
      {
          e.printStackTrace();
      }
  }
  
  public static Statement createStatement(Connection connection)
  {
      Statement statement = null;
      try
      {    
          statement = connection.createStatement();
      } catch (SQLException e)
      {
          e.printStackTrace();
      }
      return statement;
  }
  public static void closeStatement(Statement statement)
  {
      try
      {
          if(statement != null)
          {
              statement.close();
              statement = null;
          }
      }  catch (SQLException e)
      {
          e.printStackTrace();
      }
  }
  public static ResultSet getResultSet(Statement statement,String sql)
  {
      ResultSet resultSet = null;
      try
      {    
          resultSet = statement.executeQuery(sql);
      } catch (SQLException e)
      {
          e.printStackTrace();
      }
      return resultSet;
  }
  public static void closeResultSet(ResultSet resultSet)
  {
      try
      {
          if(resultSet != null)
          {
              resultSet.close();
              resultSet = null;
          }
      }  catch (SQLException e)
      {
          e.printStackTrace();
      }
  }

}